package com.shangqing.system.service.impl;

import com.shangqing.system.domain.caoyuan.Alert;
import com.shangqing.system.domain.caoyuan.Caoyuan;

import com.shangqing.system.domain.caoyuan.Vegetation;
import com.shangqing.system.domain.caoyuan.vo.*;
import com.shangqing.system.mapper.*;
import com.shangqing.system.service.CaoyuanService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.util.ArrayList;

import java.util.List;

/**
 * 草原的相关逻辑
 */
@Service
public class CaoyuanServiceImpi implements CaoyuanService {
    @Autowired
    private CaoyuanMapper caoyuanMapper;
    @Autowired
    private AlertMapper alertMapper;
    @Autowired
    private FireratingMapper fireratingMapper;
    @Autowired
    private SpeciesMapper speciesMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private VegetationCaoyuanMapper vegetationMapper;

    /**
     * 获取灾情信息
     *
     * @return
     */

    @Override
    public List<Caoyuan> getinfo() {

        List<Caoyuan> caoyuanList = caoyuanMapper.getinfo();
        List<Caoyuan> caoyuans = new ArrayList<>();

        for (Caoyuan caoyuan : caoyuanList
        ) {
            caoyuan.setAlert(alertMapper.select(caoyuan.getAlertId())); // 获取预警
            caoyuan.setFirerating(fireratingMapper.select(caoyuan.getFireratingId()));//获取级别
            caoyuan.setSpecies(speciesMapper.select(caoyuan.getSpeciesId()));// 火种类型
            CityVo city = cityMapper.select(caoyuan.getCityId());//获取当前地址
            city.setCities(cityVos(city));// 递归获取地址
            caoyuan.setCities(city);
            caoyuans.add(caoyuan);
        }
        return caoyuans;
    }

    private CityVo cityVos(CityVo list) {
        if (list.getParentId() == 0) {
            return null;      //递归出口
        } else {

            CityVo vo = cityMapper.select(list.getParentId());//获取父ID对应的地址
            CityVo cityVo = new CityVo();//封装
            cityVo.setId(vo.getId());
            cityVo.setCityName(vo.getCityName());
            cityVo.setParentId(vo.getParentId());
            cityVo.setCities(cityVos(vo)); //进行递归进口
            return cityVo;
        }
    }

    /**
     * 全区森林草原火灾灾情种类统计
     *
     * @return
     */
    @Override
    public List<SpeciesVo> getspecies() {
        List<Caoyuan> caoyuanList = caoyuanMapper.getspecies();
        List<SpeciesVo> speciesVos = new ArrayList<>();
        for (Caoyuan caoyuan : caoyuanList
        ) {
            SpeciesVo speciesVo = new SpeciesVo();
            speciesVo.setSpeciesId(caoyuan.getSpeciesId());
            speciesVo.setSpeciesName(speciesMapper.selectByName(caoyuan.getSpeciesId()));
            speciesVo.setCount(caoyuan.getCount());
            speciesVos.add(speciesVo);
        }
        return speciesVos;
    }

    /**
     * 按照月查
     *
     * @return
     */
    @Override
    public List<AlertVo> getalert() {
        List<Caoyuan> caoyuanList = caoyuanMapper.getalert(1);
        List<AlertVo> alertVos = new ArrayList<>();
        for (Caoyuan caoyuan : caoyuanList
        ) {
            AlertVo alertVo = new AlertVo();
            alertVo.setMonth(1);
            alertVo.setAlertId(caoyuan.getAlertId());
            alertVo.setWarninglevel(alertMapper.selectByName(caoyuan.getAlertId()));
            alertVo.setCount(caoyuan.getCount());
            alertVos.add(alertVo);
        }
        return alertVos;
    }


    /**
     * @return
     */
    @Override
    public List<CityVo> getcity() {

        List<CityVo> resultlist = new ArrayList<>();
        Integer count = caoyuanMapper.selectcount();//查询这些区级的一共出现多少次灾害
        List<Integer> list = caoyuanMapper.selectcishu();//查询一共有几个区级
        //百分比小数点控制
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(0);

        if (list != null) {
            for (Integer inte : list
            ) {
                //根据区级查有多少条灾害信息
                Integer countt = caoyuanMapper.selectc(inte);
                //根据区号查询区名
                String str = cityMapper.selectByid(inte);
                if (countt != null && count != null) {
                    String string = numberFormat.format((float) countt / (float) count * 100) + "%";
                    CityVo city = new CityVo();
                    city.setId(inte);
                    city.setCityName(str);
                    city.setBaifbi(string);
                    resultlist.add(city);
                }
            }
        }
        return resultlist;
    }


    /**
     * 各区灾情统计
     *
     * @return
     */

    @Override
    public List<Cityandalert> getcityandspecies() {
        List<Cityandalert> resultlist = new ArrayList<>();

        List<Integer> list = caoyuanMapper.selectcishu();//查询一共有几个区级
        for (Integer inte : list
        ) {
            Cityandalert cityandspecies = new Cityandalert();
            cityandspecies.setId(inte);
            String integer = cityMapper.selectByid(inte);     //查询每个地区名
            cityandspecies.setCityName(integer);
            List<Integer> clist = caoyuanMapper.cityandspecies(inte);  //查询每个地区的预警
            List<Alert> alertList = new ArrayList<>();
            for (Integer cc : clist
            ) {
                String str = alertMapper.selectByName(cc); //查询每个预警的名称
                Integer yujing = caoyuanMapper.selectyujing(inte, cc);//查询每个预警的次数
                Alert alert = new Alert();
                alert.setId(cc);
                alert.setWarninglevel(str);
                alert.setCount(yujing);
                alertList.add(alert);
            }
            cityandspecies.setList(alertList);
            resultlist.add(cityandspecies);
        }
        return resultlist;
    }

    @Override
    public List<VegetationVo> getcityandvegetation() {

        List<VegetationVo> resultlist = new ArrayList();
        List<Integer> list = vegetationMapper.selectcityid();//查询有哪些区号
        //百分比小数点控制
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(0);
        for (Integer cityId : list
        ) {
            String integer = cityMapper.selectByid(cityId);     //查询每个地区名
            VegetationVo vegetationVo = new VegetationVo();
            vegetationVo.setId(cityId);
            vegetationVo.setCityName(integer);
            List<Integer> inte = vegetationMapper.selectvegetationid(cityId); //查询有哪些每个区号里有那些草原类型号
            Integer count = vegetationMapper.selectcount(cityId);   //查询每个区号的总面积
            List<Vegetation> vegetations = new ArrayList<>();
            for (Integer vegetationid : inte
            ) {
                Integer countt = vegetationMapper.selectcaoyuanleixing(cityId, vegetationid);  //查询每种草原的面积
                String str = vegetationMapper.selectById(vegetationid);
                if (countt != null && count != null) {
                    String string = numberFormat.format((float) countt / (float) count * 100) + "%";

                    Vegetation vegetation = new Vegetation();
                    vegetation.setId(vegetationid);
                    vegetation.setName(str);
                    vegetation.setCount(string);
                    vegetations.add(vegetation);
                }

            }
            vegetationVo.setVegetationList(vegetations);
            resultlist.add(vegetationVo);
        }
        return resultlist;
    }


}


